package com.ggh.jinjilive.ui.mine.personal;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.databinding.ActivityCompileBinding;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.CommonUtils;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.ggh.jinjilive.utils.GlideEngine;
import com.ggh.jinjilive.utils.loginIMUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.huantansheng.easyphotos.EasyPhotos;
import com.huantansheng.easyphotos.callback.SelectCallback;
import com.huantansheng.easyphotos.models.album.entity.Photo;
import com.tencent.imsdk.v2.V2TIMUserFullInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Route(path = ARouterUtils.CompileDataActivity)
public class CompileDataActivity extends BaseActivity<ActivityCompileBinding> implements View.OnClickListener{

    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).keyboardEnable(true).init();
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_compile;
    }

    @Override
    public void initView() {
        super.initView();
        //店铺名称
        setWhiteActionBarStyle("编辑资料",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
    }

    @Override
    public void initData() {
        super.initData();
        if(null != userMobile){
            //图标
            Glide.with(binding.advocateSeeding).load(userMobile.getHead_portrait()).into(binding.advocateSeeding);
            //装扮
            Glide.with(binding.ivMinePersonTou).load(userMobile.getHead_frame()).into(binding.ivMinePersonTou);
            //用户名
            binding.tvPersonNickname.setText(userMobile.getNickname());
            //个人签名
            binding.tvPersonAutograph.setText(userMobile.getIntroduce());
            //生日
            binding.minePersonDateTv.setText(userMobile.getBirthday());
            //性别
            if (userMobile.getSex() == 1) {
                binding.tvPersonSex.setText("男");
            } else {
                binding.tvPersonSex.setText("女");
            }
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //头像修改
        binding.llProfilePhoto.setOnClickListener(this);
        //生日
        binding.llBirthday.setOnClickListener(this);
        //装扮
        binding.llDressUp.setOnClickListener(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //用户信息
        userMobile = MerchantSession.getInstance().getUser();
        if(null != userMobile){
            //装扮
            Glide.with(binding.ivMinePersonTou).load(userMobile.getHead_frame()).into(binding.ivMinePersonTou);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_profile_photo://修改头像
                EasyPhotos.createAlbum(CompileDataActivity.this, true,false, GlideEngine.getInstance()).setFileProviderAuthority("com.ggh.jinjilive.fileprovider").start(new SelectCallback() {
                    @Override
                    public void onResult(ArrayList<Photo> photos, boolean isOriginal) {
                        if(photos.size() > 0){
                            GeneralUtils.uploadimages(photos.get(0).path, new AddOnClickListener1() {
                                @Override
                                public void onSkipClicked(Object mObject) {
                                    String uploadPicture = (String) mObject;
                                    if(null != uploadPicture){
                                        Glide.with(binding.advocateSeeding).load(uploadPicture).into(binding.advocateSeeding);
                                        V2TIMUserFullInfo v2TIMUserFullInfo = new V2TIMUserFullInfo();
                                        v2TIMUserFullInfo.setNickname(userMobile.getNickname());
                                        v2TIMUserFullInfo.setFaceUrl(uploadPicture);
                                        //修改个人资料
                                        loginIMUtils.setSelfInfo(v2TIMUserFullInfo);
                                        Map<String, Object> params = new HashMap<>();
                                        //上传图片
                                        params.put("head_portrait", uploadPicture);
                                        GeneralUtils.infoSaveheadportrait(params);
                                    }
                                }
                            });
                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                break;
            case R.id.ll_birthday://生日
                CommonUtils.dateTimePickerView(mContext,binding.minePersonDateTv);
                break;
            case R.id.ll_dressUp://装扮
                ARouter.getInstance().build(ARouterUtils.DressUpActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
        }
    }
}
